package com.vshop.controller.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.comentity.Continent;
import com.vshop.entity.interfacerequest.ExceptionRequest;
import com.vshop.entity.interfacerequest.continentlist.request.RequestContinentList;
import com.vshop.entity.interfacerequest.continentlist.request.RequestContinentListData;
import com.vshop.entity.interfacerequest.continentlist.response.ResponseContinentList;
import com.vshop.entity.interfacerequest.continentlist.response.ResponseContinentListData;
import com.vshop.entity.interfacerequest.continentlist.response.ResponseContinentListTradeData;
import com.vshop.service.ContinentService;
import com.vshop.service.SequenceService;
import com.vshop.utils.Md5Encrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 大洲列表
 *
 * @author lm
 * @date 2019/03/07
 */
@RestController
@Api(description = "大洲列表")
@Slf4j
public class ContinentListController {
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private ContinentService continentService;

    @CrossOrigin
    @PostMapping(value = "continentList", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "大洲列表", response = ResponseContinentList.class)
    public String continentList(@ApiParam @RequestBody RequestContinentList request) {
        // 1，生成流水号
        String connSeqNo = sequenceService.getPackageTaskNo();
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());
        //3,定义要回复的内容
        String jsonString;
        ExceptionRequest utils = new ExceptionRequest();

        String requestStr = JSON.toJSONString(request);
        if (requestStr == null) {
            log.info("请求报文不合法:requestStr为空");
            jsonString = utils.dataWeiKong(connSeqNo, curtime);
            return jsonString;
        } else {
            log.info("前端请求报文是：" + requestStr);
            RequestContinentList requestPackage = JSONArray.parseObject(requestStr, RequestContinentList.class);
            String requestSign = requestPackage.getSign();
            if (requestPackage.getSign() == null) {
                log.info("请求报文不合法:sign为空");
                jsonString = utils.dataWeiKong(connSeqNo, curtime);
                return jsonString;
            } else {
                log.info("请求的签名是：" + requestSign);
                RequestContinentListData requestData = requestPackage.getData();
                if (requestPackage.getData() == null) {
                    log.info("请求报文不合法:data为空");
                    jsonString = utils.dataWeiKong(connSeqNo, curtime);
                    return jsonString;
                } else {
                    String ConnSeqNo = requestData.getConnSeqNo();
                    String partnerCode = requestData.getPartnerCode();
                    String tradeTime = requestData.getTradeTime();
                    String tradeType = requestData.getTradeType();
                    String lang = requestData.getLang();
                    String version = requestData.getVersion();


                    if (StringUtils.isBlank(ConnSeqNo) || StringUtils.isBlank(partnerCode) || StringUtils.isBlank(tradeTime) || StringUtils.isBlank(tradeType) || lang == null || version == null) {
                        log.info("请求报文不合法:data参数不合法");
                        jsonString = utils.dataWeiKong(connSeqNo, curtime);
                        return jsonString;
                    } else {
                        // 将请求体json转成String// 将Str拿去计算签名
                        String str = JSON.toJSONString(requestData);
                        String sign = utils.calculateSignature(str);
                        log.info("计算的签名是：" + sign);

                        if (lang.equals("cn") || StringUtils.isBlank(lang)) {
                            if (!requestSign.equalsIgnoreCase(sign)) {
                                log.info("签名验证失败！");
                                jsonString = utils.signatureFailure(ConnSeqNo, tradeTime, tradeType, "0001", "签名有误，请确认签名！");
                                return jsonString;
                            } else {
                                //签名验证成功
                                //用partnerCode和continent去数据库查询这个大洲列表
                                List<Continent> ContinentList = continentService.queryAllContinent();
                                List<ResponseContinentListTradeData> tradeDataList = new ArrayList<ResponseContinentListTradeData>();
                                ResponseContinentListTradeData tradeData = null;
                                if (ContinentList != null && !ContinentList.isEmpty()) {
                                    for (Continent c : ContinentList) {
                                        String continent_name = c.getContinent_name();
                                        tradeData = new ResponseContinentListTradeData();
                                        tradeData.setContinentName(continent_name);
                                        tradeDataList.add(tradeData);
                                    }
                                    //返回
                                    ResponseContinentListData data = new ResponseContinentListData();
                                    data.setConnSeqNo(ConnSeqNo);
                                    data.setPartnerCode(partnerCode);
                                    data.setTradeType(tradeType);
                                    data.setTradeTime(tradeTime);
                                    data.setTradeRstCode("0000");
                                    data.setTradeRstMessage("成功");
                                    data.setTradeData(tradeDataList);

                                    ResponseContinentList re = new ResponseContinentList();
                                    re.setData(data);
                                    re.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                    jsonString = JSON.toJSONString(re);
                                    log.info("响应报文：" + jsonString);
                                    return jsonString;

                                } else {
                                    log.info("接口返回出现问题");
                                    jsonString = utils.unknownError(ConnSeqNo, curtime, tradeType);
                                    return jsonString;
                                }


                            }

                        } else {//英文
                            if (!requestSign.equalsIgnoreCase(sign)) {
                                log.info("签名验证失败！");
                                jsonString = utils.signatureFailure(ConnSeqNo, tradeTime, tradeType, "0001", "The signature is wrong, please confirm the signature!");
                                return jsonString;
                            } else {
                                //签名验证成功
                                //用partnerCode和continent去数据库查询这个大洲列表
                                List<Continent> ContinentList = continentService.queryAllContinent();
                                List<ResponseContinentListTradeData> tradeDataList = new ArrayList<ResponseContinentListTradeData>();
                                ResponseContinentListTradeData tradeData = null;

                                if (ContinentList != null && !ContinentList.isEmpty()) {

                                    for (Continent c : ContinentList) {
                                        String continent_name_en = c.getContinent_name_en();
                                        tradeData = new ResponseContinentListTradeData();
                                        tradeData.setContinentName(continent_name_en);
                                        tradeDataList.add(tradeData);
                                    }
                                    //返回
                                    ResponseContinentListData data = new ResponseContinentListData();
                                    data.setConnSeqNo(ConnSeqNo);
                                    data.setPartnerCode(partnerCode);
                                    data.setTradeType(tradeType);
                                    data.setTradeTime(tradeTime);
                                    data.setTradeRstCode("0000");
                                    data.setTradeRstMessage("success");
                                    data.setTradeData(tradeDataList);

                                    ResponseContinentList re = new ResponseContinentList();
                                    re.setData(data);
                                    re.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                    jsonString = JSON.toJSONString(re);
                                    log.info("响应报文：" + jsonString);
                                    return jsonString;

                                } else {
                                    log.info("接口返回出现问题");
                                    jsonString = utils.unknownErrorEN(ConnSeqNo, curtime, tradeType);
                                    return jsonString;
                                }


                            }

                        }
                    }
                }
            }
        }
    }

}


